'''用户注册序列化类'''

from rest_framework import serializers
from .models import User

class RegisterSerializers(serializers.ModelSerializer):
    phone = serializers.CharField(max_length=11,min_length=11,required=True)
    password = serializers.CharField(max_length=128,min_length=3,required=True)
    nickname = serializers.CharField(required=False)
    gender = serializers.CharField(required=False)
    job_title= serializers.CharField(required=False)
    introduction = serializers.CharField(required=False)
    avatar = serializers.CharField(required=False)

    #验证手机号是否已经注册
    def validate_phone(self, value):
        try:
            User.objects.get(phone=value)
            raise serializers.ValidationError('手机号已经注册')
        except User.DoesNotExist:
            return value
        return value
    class Meta:
        #序列化的字段映射到User上
        model = User
        #反序列化User的所有字段
        fields = '__all__'

#获取用户详细信息的序列化器
class UserDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        #不序列化的字段
        exclude = ('create_at','update_at','_password')

